/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * <p>Project: jse2302 - FileUtils
 * <p>Powered by webrx On 2023-06-26 17:18:28
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class FileUtils {


    public static long fileCount;
    public static long rowsCount;

    /**
     * 递归遍历目录包括子目所有.java文件
     *
     * @param path
     */
    public static void getFiles(File path) {
        File[] fs = path.listFiles();
        for (File f : fs) {
            if (f.isDirectory()) {
                getFiles(f);
            } else if (f.isFile() && f.getName().endsWith(".java")) {
                ++fileCount;
                rowsCount += getFileRows(f.getAbsolutePath());
                System.out.println(f.getAbsolutePath() + String.format("【行数：%d行】", getFileRows(f.getAbsolutePath())));
            }
        }
    }

    /**
     * 返回一个文件多少行long数字,文件必须是ascii码文件
     *
     * @param file
     * @return
     */
    public static long getFileRows(String file) {
        long count = 0;
        try {
            var fis = new FileInputStream(file);
            var txt = new String(fis.readAllBytes());
            count = txt.lines().count();
            fis.close();
        } catch (Exception e) {
        }
        return count;
    }


    public static void delTree(File dir) {
        if (dir.isDirectory()) {
            File[] fs = dir.listFiles();
            for (File f : fs) {
                if (f.isDirectory()) delTree(f);
                f.delete();
            }
        }
        dir.delete();
    }

    public static void delTree(String dir) {
        delTree(new File(dir));
    }


    public static void delTree(List<String> dir) {
        for (String f : dir) {
            delTree(f);
        }
    }

    public static void copyFile(File src, File dst) {
        byte[] buf = new byte[10240];
        try (FileInputStream fis = new FileInputStream(src);
             FileOutputStream fos = new FileOutputStream(dst);
        ) {
            int len = 0;
            while ((len = fis.read(buf)) > 0) {
                fos.write(buf, 0, len);
            }

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void copyFile(String src, String dst) {
        copyFile(new File(src), new File(dst));
    }


    public static void copyDir(File src, File dst) {
        if (!src.isDirectory()) {
            return;
        }
        if (dst.exists() && dst.isDirectory()) {
            return;
        }
        dst.mkdirs();
        File[] fs = src.listFiles();
        for (File f : fs) {
            if (f.isDirectory()) {
                //是目录递归调用
                copyDir(f, new File(dst, f.getName()));
            } else if (f.isFile()) {
                //是文件复制到指定位置
                //copyFile(f,new File(dst,f.getName()));

                try {
                    var fis = new FileInputStream(f);
                    fis.transferTo(new FileOutputStream(new File(dst, f.getName())));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
